﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NexTrackDesktop.Media;

namespace NexTrackDesktop.NexTrack
{
    public abstract class MainServiceAdapter
    {
        //Functions to implement for Iteration 1
        //service.BeginHostSession, service.AddSong, service.CreateParty,
        //service.DequeueSong, service.GetNowPlaying, service.GetQueue,
        //service.RemoveSong, service.UpdateNowPlaying

        //public abstract void BeginHostSession(string FacebookId);
        public abstract void AddSong(Song song);
        public abstract string CreateParty(string description, string access_code);
        public abstract Song GetNowPlaying();
        public abstract List<QueueItem> GetQueue();
        public abstract void RemoveSong(Song song);
        public abstract void UpdateNowPlaying(Song song);

        protected class QueueComparer : IComparer<QueueItem>
        {
            public int Compare(QueueItem x, QueueItem y)
            {
                int votedif = x.NetVotes - y.NetVotes;
                return -votedif;
            }
        }

        protected List<Song> songs = new List<Song>();
        protected List<QueueItem> queue = new List<QueueItem>();

        public virtual Song GetNextSong()
        {
            queue.Sort(new QueueComparer());

            if(queue.Count == 0)
            {
                Random r = new Random();
                var shuffled = songs.OrderBy(s => r.NextDouble());
                return shuffled.First();
            }

            return queue[0].Song;
        }
    }
}
